package com.alien.admin.modules.deploy.service.impl;

import com.alien.admin.modules.deploy.domain.DeployStatisticParam;
import com.alien.admin.modules.deploy.service.DeployRecService;
import com.alien.admin.modules.deploy.service.DeployStatisticService;
import com.alien.admin.modules.deploy.service.DpUserServerService;
import com.alien.admin.modules.deploy.service.dto.DeployRecQueryCriteria;
import com.alien.admin.modules.deploy.service.dto.DeployStatisticDto;
import com.alien.admin.modules.publisher.service.PublishSheetFlowService;
import com.alien.admin.utils.SecurityUtils;
import com.google.common.collect.Lists;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * Copyright (C), 2014-2021, 厦门神州鹰软件科技有限公司
 * FileName: DeployStatisticServiceImpl
 *
 * @author: zengzf
 * Date:     2021/11/12 11:04
 * Description:
 * History:
 */
@Service
@RequiredArgsConstructor
@Slf4j
public class DeployStatisticServiceImpl implements DeployStatisticService {

    private final DpUserServerService dpUserServerService;
    private final PublishSheetFlowService publishSheetFlowService;
    private final DeployRecService deployRecService;

    @Override
    public DeployStatisticDto getStatisticInfo(DeployStatisticParam deployStatisticParam) {

        Long currentUserId = SecurityUtils.getCurrentUserId();
        Integer serverCount = dpUserServerService.countServerByUserId(currentUserId);
        Integer approvalCount = publishSheetFlowService.toDealCountByUserId(currentUserId);
        Integer publishTodayCount = publishSheetFlowService.oneDayPublishCount(null);

        DeployRecQueryCriteria criteria = new DeployRecQueryCriteria();
        criteria.setEnv(deployStatisticParam.getEnv());
        if (deployStatisticParam.getBeginTime() != null && deployStatisticParam.getEndTime() != null) {
            criteria.setDeployTime(Lists.newArrayList(deployStatisticParam.getBeginTime(), deployStatisticParam.getEndTime()));
        }
        Integer deployCount = deployRecService.getDeployCount(criteria);

        DeployStatisticDto deployStatisticDto = new DeployStatisticDto();
        deployStatisticDto.setApprovalCount(approvalCount);
        deployStatisticDto.setServerCount(serverCount);
        deployStatisticDto.setPublishTodayCount(publishTodayCount);
        deployStatisticDto.setDeployCount(deployCount);

        return deployStatisticDto;
    }

}

